import axios from 'axios';
import { getToken,removeToken } from "@/store/localStorage/cookies";
//import router from 'vue-router'
//import qs from 'qs';

//自动切换环境
axios.defaults.baseURL =  process.env.VUE_APP_BASE_API

//设置超时时间
axios.defaults.timeout = process.env.VUE_APP_TIMEOUT;
axios.defaults.withcredentials = true

//请求拦截(请求发出前处理请求)
axios.interceptors.request.use(config => {
    /*解决跨域问题 */
    config.headers.common["Access-Control-Allow-Origin"] = "*";
    config.headers.common["Access-Control-Allow-Methods"] = "*";
    config.headers.common["Access-Control-Allow-Credentials"] = true;

    //在发送请求之前如果为post序列化请求参数
    /* if (config.method === 'post') {
        config.data = QS.stringify(config.data);
        axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
        
    }  */
    if (getToken()) {
        //token字段是要和后端协商好的
        config.headers.common["Authorization"] = getToken();
        // debugger
    }
    return config;
}, (error) => {
    return Promise.reject(error);
});

// 响应拦截器（处理响应数据）
axios.interceptors.response.use((res) => {
    //对响应数据做判断，与后台协议统一接口返回格式
    //console.log('>>>>>>>response: ', res);

    if (res.status != 200) { //这个判断可根据实际情况修改
        return Promise.reject(res);
    }

    return res;
}, (error) => {
    console.log(error)
    if(error.response.status == 401 || error.status == 0) {
        removeToken();
    }
    console.log(error);
    return Promise.reject(error);
});

// 封装get方法
function Get(url, data){
    return new Promise((resolve, reject) => {
        axios.defaults.headers.get['Content-Type'] = 'application/json;charset=UTF-8';
        //console.log('>>>>>>>request',data);
        axios.get(url, { params: data }).then(res =>{
            //console.log('>>>>>>>>>>>>执行结果',res);
            resolve(res.data);
        }).catch(err => {
            //console.log('>>>>>>>>>>>>>错误！',err)
            reject(err.data);
        })
    });
}

// 封装post方法
function Post(url, params){
    //console.log('>>>>>>>>>>>>>>>Post',params)
    
    return new Promise((resolve, reject) =>{
        axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
        axios.post(url, params).then(res =>{
            resolve(res.data);
        }).catch(err =>{
            reject(err.data);
        })
    });
}

// 封装delete方法
function Delete(url, params){
    axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
    return new Promise((resolve, reject) =>{
        axios.delete(url,{params: params}).then(res =>{
            resolve(res.data);
        }).catch(err =>{
            //console.log('>>>>>>>>>>>>>>err',err.message);
            reject(err.data);
        })
    });
}

// 封装Put方法
function Put(url, params){
    return new Promise((resolve, reject) =>{
        axios.defaults.headers.put['Content-Type'] = 'application/json;charset=UTF-8';
        axios.put(url, params).then(res =>{
            resolve(res.data);
        }).catch(err =>{
            reject(err.data);
        })
    });
}

//封装Patch方法
function Patch(url, params){
    return new Promise((resolve, reject) =>{
        axios.defaults.headers.patch['Content-Type'] = 'application/json;charset=UTF-8';
        axios.patch(url, params).then(res =>{
            resolve(res.data);
        }).catch(err =>{
            reject(err.data);
        })
    });
}



//对外接口
export function request({method, url, data}){
    //console.log(axios.defaults)
    //console.log('>>>>>>>>>>>request_method_data',data)
    if(method == 'get'){
        return Get(url, data);
    }else if(method == 'post'){
        return Post(url, data);
    }else if(method == 'delete'){
        return Delete(url, data);
    }else if(method == 'put'){
        return Put(url, data);
    }else if(method == 'patch'){
        return Patch(url, data);
    }
}

export default {
    install(Vue) {
    Vue.prototype.$axios = axios;
    Vue.prototype.$request = function () {
            return request;
        }
    }
}